package com.future.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.future.domain.Repair;
import com.future.domain.Room;
import com.future.domain.Storey;
import com.future.dto.AroomNameDto;
import com.future.dto.MsgDto;
import com.future.dto.RepairDto;
import com.future.service.RepairService;
import com.future.service.RoomService;
import com.future.service.StoreyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.List;

@RestController
@RequestMapping("repair")
public class RepairController {

    @Autowired
    RepairService repairService;

    @Autowired
    StoreyService storeyService;

    @Autowired
    RoomService roomService;

    //查询宿舍保修
    @RequestMapping("repairAll")
    public Page<Repair> repairAll(RepairDto repairDto){

        QueryWrapper<Repair> queryWrapper = new QueryWrapper<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        if(repairDto.getRepairdate()==null & repairDto.getRepairetime()==null){
            queryWrapper.lambda()
                    .eq(Repair::getRepairstatus,repairDto.getRepairstatus());
        }else {

            String format = sdf.format(repairDto.getRepairdate());
            String format2 = sdf.format(repairDto.getRepairetime());
            queryWrapper.lambda()
                    .ge(Repair::getRepairdate,format)
                    .le(Repair::getRepairetime,format2)
                    .eq(Repair::getRepairstatus,repairDto.getRepairstatus());
        }



        //倒叙排序
        queryWrapper.orderByAsc("repairid");

        Page<Repair> page = new Page<>(repairDto.getCurrent(),repairDto.getSize());
        this.repairService.page(page,queryWrapper);


        return page;


    }



    //查询楼栋
    @RequestMapping("findStorey")
    public MsgDto findStorey(){
        List<Storey> storey = this.storeyService.list();

        return new MsgDto(true,"查询成功",storey);
    }

    // 根据所选楼栋id查询该楼栋下所有的宿舍
    @RequestMapping("findRoom")
    public MsgDto findRoom(String storeyname){

        List<AroomNameDto> room = this.roomService.findRoom(storeyname);
        return new MsgDto(true,"查询成功",room);
    }

    //添加
    @RequestMapping("addRepair")
    public MsgDto addRepair(@RequestBody Repair repair){

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        sdf.format(repair.getRepairdate());
        sdf.format(repair.getRepaircreatetime());
        this.repairService.save(repair);
        return new MsgDto(true,"添加成功");
    }

    //审核
    @RequestMapping("updateRepair")
    public MsgDto updateRepair(@RequestBody Repair repair){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        sdf.format(repair.getRepairetime());
        this.repairService.updateById(repair);
        return  new MsgDto(true,"修改成功");
    }
}
